# -*- coding: utf-8 -*-
from django.http import HttpResponse
from openpyxl.writer.excel import save_virtual_workbook

from demo_common_module.utils.excel_utils import ExcelUtils


class ResultUtils(object):

    @classmethod
    def excel_export_response(cls, sheet_name, data_list):
        wb = ExcelUtils.export_excel(sheet_name, data_list)
        file_name = '{}.xlsx'.format(sheet_name)
        response = HttpResponse(content=save_virtual_workbook(wb))
        response['Content-Type'] = 'application/octet-stream;'
        response['Access-Control-Expose-Headers'] = 'Content-Disposition'
        file_name = file_name.encode('utf-8').decode('ISO-8859-1')
        response['Content-Disposition'] = f'attachment; filename={file_name}'

        return response

    @classmethod
    def process_result(cls, result):
        for item in result:
            item['create_time'] = item['create_time'][:19].replace('T', ' ')
            item['update_time'] = item['update_time'][:19].replace('T', ' ')

    @classmethod
    def get_result_by_page(cls, result, page, num):
        page = int(page)
        num = int(num)
        return result[(page - 1) * num: page * num], len(result)
